package com.dxyxx.framework;

import org.apache.http.Header;
import org.codehaus.jackson.type.JavaType;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import br.com.pierry.simpletoast.SimpleToast;

import com.android.customized.CountDownButtonHelper;
import com.android.customized.CountDownButtonHelper.OnFinishListener;
import com.dxyxx.R;
import com.dxyxx.dialog.AgainPlayer;
import com.dxyxx.dialog.WaittingCircle;
import com.dxyxx.pojo.ResponseResult;
import com.dxyxx.pojo.SmsType;
import com.http.HttpUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ta.utdid2.android.utils.StringUtils;

public class SmsLoginActivity extends BaseActivity {

	private EditText phone;
	private EditText code;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_smslogin);
		context = this;
		phone = (EditText) findViewById(R.id.phone);
		phone.setSelectAllOnFocus(true);
		phone.setFocusable(true);
		phone.setFocusableInTouchMode(true);

		code = (EditText) findViewById(R.id.code);
		code.setSelectAllOnFocus(true);
		code.setFocusable(true);
		code.setFocusableInTouchMode(true);

		initViews();

	}

	private Button codeButton;
	private Button loginButton;

	private void initViews() {
		((ImageView) findViewById(R.id.backImageButton)).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView titleView = (TextView) findViewById(R.id.title);
		titleView.setText("手机验证码登陆");
		titleView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
		// 点击发送验证码。。等待30秒
		codeButton = (Button) findViewById(R.id.b_send);
		loginButton = (Button) findViewById(R.id.b_login);
		codeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr)) {
					SimpleToast.error(context, "手机号不能为空");
					return;
				} else {
					// 发送http 请求
					String url = "sms/send";
					RequestParams rp = new RequestParams();
					rp.put("phone", phoneStr);
					rp.put("type", SmsType.Login.toString());
					// 禁止 按钮点击了。
					codeButton.setClickable(false);
					HttpUtil.post(url, rp, new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							try {
								JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, String.class);
								ResponseResult<String> rr = getObjectMapper().readValue(arg2, javaType);

								//{"object":null,"code":1,"info":"验证码已发送，稍等"}
								if (rr.getCode() == 1) { //成功
									// 启动 倒时计数器
									CountDownButtonHelper helper = new CountDownButtonHelper(codeButton, "发送验证码", 30, 1);
									helper.setOnFinishListener(new OnFinishListener() {

										@Override
										public void finish() {
											Toast.makeText(SmsLoginActivity.this, "倒计时结束", Toast.LENGTH_SHORT).show();
											codeButton.setClickable(true);//激活 按钮
										}
									});
									helper.start();
									// 显示信息
									SimpleToast.ok(context, rr.getInfo());
								} else { //失败 
									// 显示信息
									SimpleToast.info(context, rr.getInfo());
									codeButton.setClickable(true);//激活 按钮
								}
							} catch (Exception e) {
								e.printStackTrace();
								SimpleToast.error(context, "系统内部出错！");
								codeButton.setClickable(true);//激活 按钮
							}
						}

						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
							AgainPlayer.makeCall((Activity) context, "网络好像出错了");
							codeButton.setClickable(true);//激活 按钮
						}
					});
				}

			}
		});

		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 判断手机号 是否填写正确
				String phoneStr = phone.getText().toString().trim();
				String codeStr = code.getText().toString().trim();
				if (StringUtils.isEmpty(phoneStr) || StringUtils.isEmpty(codeStr)) {
					SimpleToast.error(context, "手机号|验证码 不能为空");
					return;
				} else {
					// 发送http 请求
					String url = "sms/smslogin2";
					RequestParams rp = new RequestParams();
					rp.put("phone", phoneStr);
					rp.put("code", codeStr);
					// 禁止 登陆按钮点击了。
					loginButton.setClickable(false);
					// 启动等待框
					ProgressDialog progressDialog = WaittingCircle.showDialog(context, false);

					HttpUtil.post(url, rp, new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
							try {
								JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(ResponseResult.class, String.class);
								ResponseResult<String> rr = getObjectMapper().readValue(arg2, javaType);

								//{"object":null,"code":1,"info":"验证码已发送，稍等"}
								if (rr.getCode() == 1) { //成功
									// 显示信息
									SimpleToast.ok(context, rr.getInfo());
								} else { //失败 
									// 显示信息
									SimpleToast.info(context, rr.getInfo());
									loginButton.setClickable(true);//激活 按钮
								}
							} catch (Exception e) {
								e.printStackTrace();
								SimpleToast.error(context, "系统内部出错！");
								loginButton.setClickable(true);//激活 按钮
							}
						}

						@Override
						public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
							AgainPlayer.makeCall((Activity) context, "网络好像出错了");
							loginButton.setClickable(true);//激活 按钮
						}
					});
				}

			}
		});
	}
}
